package com.isunland.managesystem.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.CompanyForumTopic;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.widget.RoundImageView;
import java.util.Date;
import java.util.List;

/**
 * 话题信息类
 *
 * @author wu
 */
public class CompanyForumTopicListAdapter extends ArrayAdapter<CompanyForumTopic.actualObject> {

  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvTitleForum;
    public final TextView tvContentForum;
    public final RoundImageView ivImageForum;
    public final TextView tvNameForum;
    public final TextView tvTimeForum;
    public final TextView tvNumberForum;
    public final ImageView ivPictureAFourm;
    public final ImageView ivPictureBFourm;
    public final ImageView ivPictureCFourm;
    public final ImageView ivPictureDFourm;
    public final TextView tvFileForum;

    private ViewHolder(LinearLayout rootView, TextView tvTitleForum, ImageView ivPictureAFourm,
        ImageView ivPictureBFourm, ImageView ivPictureCFourm, ImageView ivPictureDFourm,
        TextView tvFileForum, TextView tvContentForum, RoundImageView ivImageForum,
        TextView tvNameForum, TextView tvTimeForum, TextView tvNumberForum) {
      this.rootView = rootView;
      this.tvTitleForum = tvTitleForum;
      this.tvContentForum = tvContentForum;
      this.ivImageForum = ivImageForum;
      this.tvNameForum = tvNameForum;
      this.tvTimeForum = tvTimeForum;
      this.tvNumberForum = tvNumberForum;
      this.ivPictureAFourm = ivPictureAFourm;
      this.ivPictureBFourm = ivPictureBFourm;
      this.ivPictureCFourm = ivPictureCFourm;
      this.ivPictureDFourm = ivPictureDFourm;
      this.tvFileForum = tvFileForum;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvTitleForum = (TextView) rootView.findViewById(R.id.tv_title_forum);
      TextView tvContentForum = (TextView) rootView.findViewById(R.id.tv_content_forum);
      RoundImageView ivImageForum = (RoundImageView) rootView.findViewById(R.id.iv_image_forum);
      TextView tvNameForum = (TextView) rootView.findViewById(R.id.tv_name_forum);
      TextView tvTimeForum = (TextView) rootView.findViewById(R.id.tv_time_forum);
      TextView tvNumberForum = (TextView) rootView.findViewById(R.id.tv_number_forum);
      ImageView ivPictureAFourm = (ImageView) rootView.findViewById(R.id.iv_pictureA_fourm);
      ImageView ivPictureBFourm = (ImageView) rootView.findViewById(R.id.iv_pictureB_fourm);
      ImageView ivPictureCFourm = (ImageView) rootView.findViewById(R.id.iv_pictureC_fourm);
      ImageView ivPictureDFourm = (ImageView) rootView.findViewById(R.id.iv_pictureD_fourm);
      TextView tvFileForum = (TextView) rootView.findViewById(R.id.tv_file_forum);
      return new ViewHolder(rootView, tvTitleForum, ivPictureAFourm, ivPictureBFourm,
          ivPictureCFourm, ivPictureDFourm, tvFileForum, tvContentForum, ivImageForum, tvNameForum,
          tvTimeForum, tvNumberForum);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_forum_topic_list, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    CompanyForumTopic.actualObject item = getItem(position);

    vh.tvTitleForum.setText(item.getTopicTitle());
    vh.tvContentForum.setText(item.getTopicContents());
    vh.tvNameForum.setText(item.getRegStaffName());

    // 头像
    String filePath = item.getPicture();
    if (!TextUtils.isEmpty(filePath)) {
      filePath = FileUtil.getThumbNailFilePath(filePath);
      String urlImage = ApiConst.getDownLoadUrlByGet(filePath);

      ImageListener listener = ImageLoader.getImageListener(vh.ivImageForum, // 控件名
          R.drawable.photo, R.drawable.photo); // 默认头像
      mImageLoader.get(urlImage, listener);
    } else {
      vh.ivImageForum.setImageResource(R.drawable.photo);
    }

    String pictureA = item.getTopicPicA();
    String pictureB = item.getTopicPicB();
    String pictureC = item.getTopicPicC();
    String pictureD = item.getTopicPicD();
    if ((TextUtils.isEmpty(pictureA)
        && TextUtils.isEmpty(pictureB)
        && TextUtils.isEmpty(pictureC)
        && TextUtils.isEmpty(pictureD))) {
      vh.ivPictureAFourm.setVisibility(View.GONE);
      vh.ivPictureBFourm.setVisibility(View.GONE);
      vh.ivPictureCFourm.setVisibility(View.GONE);
      vh.ivPictureDFourm.setVisibility(View.GONE);
    } else {
      volleyPostGetPic(pictureA, vh.ivPictureAFourm);
      volleyPostGetPic(pictureB, vh.ivPictureBFourm);
      volleyPostGetPic(pictureC, vh.ivPictureCFourm);
      volleyPostGetPic(pictureD, vh.ivPictureDFourm);
    }

    // 附件
    String forumFilePath = item.getTopicFile();
    if (!TextUtils.isEmpty(forumFilePath)) {
      vh.tvFileForum.setVisibility(View.VISIBLE);
      forumFilePath =
          forumFilePath.substring(forumFilePath.lastIndexOf("/") + 1, forumFilePath.length());
      vh.tvFileForum.setText(forumFilePath);
    } else {
      vh.tvFileForum.setVisibility(View.GONE);
    }

    // 服务器返回的时间
    String times = item.getRegDate();
    String publishTime = DateUtil.dateToTimeInMillis(context, times, new Date());
    vh.tvTimeForum.setText(publishTime);

    vh.tvNumberForum.setText(item.getDiscussCount());

    return vh.rootView;
  }

  private void volleyPostGetPic(String pictureUrl, ImageView imageView) {
    if (!TextUtils.isEmpty(pictureUrl)) {
      imageView.setVisibility(View.VISIBLE);
            /*没有上传缩略图*/
      //            pictureUrl = FileUtil.getThumbNailFilePath(pictureUrl);
      String urlImage = ApiConst.getDownLoadUrlByGet(pictureUrl);
      ImageListener listener = ImageLoader.getImageListener(imageView, // 控件名
          R.drawable.fail_pic, R.drawable.fail_pic); // 默认图片
      mImageLoader.get(urlImage, listener);
    } else {
      imageView.setVisibility(View.GONE);
    }
  }

  private LayoutInflater mInflater;

  private Context context;
  private ImageLoader mImageLoader;

  // Constructors
  public CompanyForumTopicListAdapter(Context context,
      List<CompanyForumTopic.actualObject> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
    mImageLoader = RequestManager.getImageLoader();
  }
}
